package com.ipay.wechat.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ipay.redis.JedisExecutor;
import com.ipay.redis.JedisKeyPrefix;
import com.ipay.utils.WxJSUtil;

@Controller
@RequestMapping(value = "/order")
public class TestScanCodeController {
	
	@Autowired
	private JedisExecutor jedies;

    /**
     * /order/testScanCode
     * @param model
     * @param requesturl
     * @return
     */
	@RequestMapping(value = "/testScanCode", method = RequestMethod.GET)
    public ModelAndView testPage(Model model,HttpServletRequest requesturl) {
		
		String ticket = jedies.getValue(JedisKeyPrefix.JEDIS_KEY_PREFIX_WECHAT_TOKEN, "jsapi_ticket");
		
		//String url =Constant.DOMAINURL + "order/testScanCode";
		String url = requesturl.getRequestURL().toString();
		System.out.println("url============"+url);
		if(!url.startsWith("https:"))    
		{    
			url = url.replaceAll("http", "https");
		} 
		System.out.println("url---------------"+url);
        Map<String, String> ret = WxJSUtil.sign(url,ticket);
         for (Map.Entry entry : ret.entrySet()) {
                System.out.println(entry.getKey() + "=" + entry.getValue());
                model.addAttribute(entry.getKey().toString(), entry.getValue());
            }
         model.addAttribute("url", url);
         return new ModelAndView("/test/testScanCode.jsp").addObject(model);
    }
	
	@RequestMapping(value = "/alipayView", method = RequestMethod.GET)
    public ModelAndView alipayView(Model model,HttpServletRequest requesturl) {

         return new ModelAndView("pay/payView.html").addObject(model);
    }
}
